#include "logic.h"
#include <string.h>
#include "OLED.h"
#include "stm32f1xx_it.h"
#include "flash.h"
#include "Usart_Scree_Show.h"

usart Usart_2_4G,Usart_Screen,Usart_Text;
Screen_Date Screen_date;
UVA_Date UVA_date;
int Flag = 0;
uint8_t Animal_N = 1;
#define LEN_HEADER 1
#define LEN_TAIL 1

uint8_t UVA_Send[40];
Animal_Date animal_date[80];
uint8_t animal_date_num = 0;

//定义，0为有效 ，1为无效加走过的格子，3为飞过的点，4~9是有动物出现的点；
//BSP与hardware封装未作好，而且没有加入回调函数，无法做到各个模块之间相互传送，任然使用的extern，这样可能会出现变量出错，还需改进.
//并且log封装无法使用，后续考虑加入，让单片机的调试可以在终端打印数据
void logic_Init(void)
{
    Usart_2_4G.usart_handle = &huart3;
    Usart_2_4G.recv_buff_size = 120;
    USARTInit(&Usart_2_4G);

    Usart_Screen.usart_handle = &huart2;
    Usart_Screen.recv_buff_size = 9;
    USARTInit(&Usart_Screen);

    // HAL_UART_Receive_IT(&huart3,wireless_rx,1);
    // wireless_initBuffer();
}

void logic_task(uint8_t Mode)
{
    if(Mode == Logic_Mode)
    {
        for(int i = 0; i < 10;i++)
        {
            UVA_Send[0] = UVA_Logic_Head;
            UVA_Send[1] = Screen_date.fly_state;
            UVA_Send[2] = UVA_Logic_Tail;
            USARTSend(&Usart_2_4G,&UVA_Send,3,USART_TRANSFER_DMA);
            HAL_Delay(20); 
        }
    }
    else if(Mode == Map_Mode)
    {
        UVA_Send[0] = UVA_Map_Head;
        UVA_Send[1] = (6-Screen_date.MAP[0])*10+(8-Screen_date.MAP[1]);
        UVA_Send[2] = (6-Screen_date.MAP[2])*10+(8-Screen_date.MAP[3]);
        UVA_Send[3] = (6-Screen_date.MAP[4])*10+(8-Screen_date.MAP[5]);   
        UVA_Send[4] = UVA_Map_Tail;
        USARTSend(&Usart_2_4G,&UVA_Send,5,USART_TRANSFER_DMA);
    }
}

void UsartSCreenData(uint8_t *buff)
{
    uint16_t judge_length; // 统计一帧数据长度

	if (buff == NULL)	   // 空数据包，则不作任何处理
		return;

     // 写入帧头数据(5-byte),用于判断是否开始存储裁判数据
	memcpy(&Screen_date.Head, buff, 1); 

    if (buff[0] == Usart_Screen_Head)
    {
        Flag++;
        buff[0] = 0;
    }
    if(Flag == 1)
    {
        Screen_date.MAP[0] = buff[1];
        Screen_date.MAP[1] = buff[2];
    }
    else if(Flag == 2)
    {
        Screen_date.MAP[2] = buff[1];
        Screen_date.MAP[3] = buff[2];
    }
    else if(Flag == 3)
    {
        Screen_date.MAP[4] = buff[1];
        Screen_date.MAP[5] = buff[2];
    }
    else if(Flag == 4 && buff[3] == Usart_Screen_Tail) 
    {
        memset(Screen_date.Data,0,sizeof(Screen_date.Data));
        Flag = 0;
    }
    //飞行状态
   if(buff[0] == Usart_Screen_Head_Fly)
    {
        // memcpy(&Screen_date.fly_state, &buff[1],1);  
        if(buff[2] == Usart_Screen_Tail && buff[1] == 0x01)
        {
            Screen_date.fly_state = Ready_Fly;
            memset(Screen_date.Data,0,sizeof(Screen_date.Data));
            __HAL_UART_CLEAR_PEFLAG(&huart2);
        }  
    }
    else if(buff[0] == Usart_Screen_Head_Clear)
    {
        memcpy(&Screen_date.clear_state,(buff + 1),1);
        if(buff[2] == Usart_Screen_Tail)
        {
            memset(Screen_date.Data,0,sizeof(Screen_date.Data));
            __HAL_UART_CLEAR_PEFLAG(&huart2);
        }
    }
    else if(buff[0] == 0xA8)
    {
        if(buff[2] == Usart_Screen_Tail && buff[1] == 0x01)
        {
            while(1)
            {
                Printf_Animal();
            }

            memset(Screen_date.Data,0,sizeof(Screen_date.Data));
            __HAL_UART_CLEAR_PEFLAG(&huart2);
        }
    }
}

void UVA_Date_Text(usart Usart,uint8_t length,uint8_t Temp[])
{
    for(int i = 0;i < length;i++)
    {
        Temp[i] = Usart_2_4G.recv_buff[0];
    }
}

uint8_t Temp[80];
uint8_t Num = 0;
//读取无人机发送的数据包检验,时间关系没有做好封装
//对于多个数据帧来说，最好的方法是使用函数的递归读取，这样你的缓存有多大，数据就可以读到多少，大大减少数据丢包的可能性
//串口的读取数据在本身的中断函数中
void Show_Text(void)
{

    uint8_t Last_x = 0,Last_y = 0,x=0,y=0;
    for(int j = 0;j<200;j++)
    {   
        if(UVA_date.Data[j] == UVA_Recive_Head)
        {
        // memcpy(&Temp,UVA_date.Data+j,80);
        Num = UVA_date.Data[j+1];
            if(UVA_date.Data[j + Num + 2] == UVA_Recive_Tail)
            {
                memcpy(&Temp,UVA_date.Data+j,80);
                memset(UVA_date.Data,0,sizeof(UVA_date.Data));
            }
        }
        else if(UVA_date.Data[j] == UVA_Recive_Animal_Head)
        {
            if(UVA_date.Data[j + 8] == UVA_Recive_Animal_Tail)
            {
                memcpy(UVA_date.Mid_Date,UVA_date.Data+j,10);
                memset(UVA_date.Data,0,sizeof(UVA_date.Data));
            }
        }
    } 
}

void Draw_Line(void)
{
    uint8_t Char_Temp;
    int X_Now,Y_Now,X_Last,Y_Last;
    for(int j = 2;j <= Num + 1;j++)
    {
        // FlashReadBuff(0x78+j,Char_Temp,1);
        Char_Temp = Temp[j];
        Y_Now =  (Char_Temp/10)*65 + 30;
        X_Now =  (Char_Temp%10)*65 + 30;
        if(X_Now != 0 && Y_Now != 0 && X_Last !=0 && Y_Last != 0)
        {
                Screen_Line(X_Last,Y_Last,X_Now,Y_Now,RED);  
                if(X_Last == X_Now && Y_Last - Y_Now > 0)//上
                {
                    Screen_Line(X_Now,Y_Now,X_Now-5,Y_Now+8,RED);
                    Screen_Line(X_Now,Y_Now,X_Now+5,Y_Now+8,RED);
                }
                else if (X_Last == X_Now && Y_Last - Y_Now < 0)//下
                {
                    Screen_Line(X_Now,Y_Now,X_Now-5,Y_Now-8,RED);
                    Screen_Line(X_Now,Y_Now,X_Now+5,Y_Now-8,RED);
                }
                else if(Y_Last == Y_Now && X_Last - X_Now > 0)//左
                {
                    Screen_Line(X_Now,Y_Now,X_Now+8,Y_Now-5,RED);
                    Screen_Line(X_Now,Y_Now,X_Now+8,Y_Now+5,RED);
                }
                else if(Y_Last == Y_Now && X_Last - X_Now < 0)//右
                {
                    Screen_Line(X_Now,Y_Now,X_Now-8,Y_Now-5,RED);
                    Screen_Line(X_Now,Y_Now,X_Now-8,Y_Now+5,RED);
                }
        }
        X_Last = X_Now;
        Y_Last = Y_Now;
    }

    memset(UVA_date.Data,0,sizeof(UVA_date.Data));
    memset(UVA_date.Data_Last,0,sizeof(UVA_date.Data_Last));
}

/**
 * @brief 处理接收到的数据，判读数据是否重复，是否超过，存储数据，有无动物等
 * @param Mid//接收数据包
 */
void Animal_Date_Processing(uint8_t Date[10])
{
    uint8_t i  = 0;
    uint8_t x = 0,y = 0;
    x =  Date[2]/10;
    y = Date[2]%10;
    if(Date[0] == UVA_Recive_Animal_Head && Date[8] == UVA_Recive_Animal_Tail)
    {
        //动物数据不为0
        if(Date[3] == 0 && Date[4] == 0 && Date[5] == 0 && Date[6] == 0 && Date[7] == 0 )
        {
        return;
        }
        if(Date[3] != 0 || Date[4] != 0 || Date[5] != 0 || Date[6] != 0 || Date[7] != 0)
        {
            for(i = 0;i<80;i++)
            {
                if(animal_date[i].Location == Date[2])
                {
                    return;
                }
                else if(
                Date[2] == animal_date[i-1].Location &&
                Date[3] == animal_date[i-1].Elephabt && 
                Date[5] == animal_date[i-1].Monkey && 
                Date[4] == animal_date[i-1].Peacock && 
                Date[6] == animal_date[i-1].Tiger && 
                Date[7] == animal_date[i-1].Wolf
                )return;
                else if(Date[3] == animal_date[i-1].Elephabt && Date[5] == animal_date[i-1].Monkey && Date[4] == animal_date[i-1].Peacock && Date[6] == animal_date[i-1].Tiger && Date[7] == animal_date[i-1].Wolf)
                {
                    if((Date[2]-1) == animal_date[i-1].Location)
                    {
                        return;
                    }
                    else if ((Date[2] + 1) == animal_date[i-1].Location)
                    {
                        return;
                    }
                    else if ((Date[2] + 10) == animal_date[i-1].Location)
                    {
                        return;
                    }
                    else if ((Date[2] - 10) == animal_date[i-1].Location)
                    {
                        return;
                    }
                     //判断是否为相邻数据
                    if((x-1) == (animal_date[i-1].Location/10) && y == (animal_date[i-1].Location%10)) return;//上
                    else if (x == (animal_date[i-1].Location/10) && (y-1) == (animal_date[i-1].Location%10))return;//左
                    else if ((x+1) == (animal_date[i-1].Location/10) && y == (animal_date[i-1].Location%10))return;//下
                    else if (x == (animal_date[i-1].Location/10) && (y+1) == (animal_date[i-1].Location%10))return;//右
                }
                else if (animal_date[i].Location == 0 && animal_date[i].Elephabt == 0 && animal_date[i].Monkey == 0 && animal_date[i].Peacock == 0 && animal_date[i].Tiger == 0 && animal_date[i].Wolf == 0)
                {
                    animal_date[i].Location = Date[2];
                    animal_date[i].Elephabt = Date[3];
                    animal_date[i].Peacock = Date[4];
                    animal_date[i].Monkey = Date[5];
                    animal_date[i].Tiger = Date[6];
                    animal_date[i].Wolf = Date[7];
                    return;
                }
            }
            
        }
    }
}

//数据判断结构体
Animal_Date animal_date_last;
uint8_t x_Last = 0,y_Last = 0;
//串口屏打印函数
void Animal_Location(uint8_t date[10])
{
    uint8_t x = 0,y = 0;
    x =  date[2]/10;
    y = date[2]%10;
    //数据为零不要
    if(date[3] == 0 && date[4] == 0 && date[5] == 0 && date[6] == 0 && date[7] == 0 )
    {
        memset(UVA_date.Mid_Date,0,sizeof(UVA_date.Mid_Date));
        return;
    }
    if(date[2] == animal_date_last.Location && date[3] == animal_date_last.Elephabt && date[4] == animal_date_last.Peacock && date[5] == animal_date_last.Monkey && date[6] == animal_date_last.Tiger && date[7] == animal_date_last.Wolf)
    {
        memset(UVA_date.Mid_Date,0,sizeof(UVA_date.Mid_Date));
        return;
    }
    if(date[3] == animal_date_last.Elephabt && date[4] == animal_date_last.Peacock && date[5] == animal_date_last.Monkey && date[6] == animal_date_last.Tiger && date[7] == animal_date_last.Wolf)
    {
        //判断是否为相邻数据
        if((x-1) == x_Last && y == y_Last) return;//上
        else if (x == x_Last && (y-1) == y_Last)return;//左
        else if ((x+1) == x_Last && y == y_Last)return;//下
        else if (x == x_Last && (y+1) == y_Last)return;//右

        if((date[2]-1) == animal_date_last.Location)
        {
        return;
        }
        else if ((date[2] + 1) == animal_date_last.Location)
        {
        return;
        }
        else if ((date[2] + 10) == animal_date_last.Location)
        {
        return;
        }
        else if ((date[2] - 10) == animal_date_last
        .Location)
        {
        return;
        }
        memset(UVA_date.Mid_Date,0,sizeof(UVA_date.Mid_Date));
    }
//沟槽的串口屏，还不允许数据转换


//这里可以让串口屏改成数字显示，就可以不用这么多判读了
//后续记得进行更改
//象
if(date[3] > 0)
{
    Screen_Num(5,y+1);
    Screen_Num(10,7-x);
    Screen_Num(0,date[3]); 
}

//孔雀
else if(date[4] > 0)
{
    Screen_Num(6,y+1);
    Screen_Num(11,7-x);
    Screen_Num(1,date[4]);
}

//猴子
else if(date[5] > 0)
{
    Screen_Num(7,y+1);
    Screen_Num(12,7-x);
    Screen_Num(2,date[5]);
}

//老虎
else if(date[6] > 0)
{
    Screen_Num(8,y+1);
    Screen_Num(13,7-x);
    Screen_Num(3,date[6]);
}

//wolf
else if(date[7] > 0)
{
    Screen_Num(9,y+1);
    Screen_Num(14,7-x);
    Screen_Num(4,date[7]);
}


    animal_date_last.Location = date[2];
    animal_date_last.Elephabt = date[3];
    animal_date_last.Peacock = date[4];
    animal_date_last.Monkey = date[5];
    animal_date_last.Tiger = date[6];
    animal_date_last.Wolf = date[7];
    x_Last =  date[2]/10;
    y_Last = date[2]%10;    
    animal_date_num++;
    memset(UVA_date.Mid_Date,0,sizeof(UVA_date.Mid_Date));
}


/**
 * @brief 打印全部动物识别的数据
 * 
 */
uint8_t E,P,M,T,W;//动物标志简称
uint8_t E_Num = 0,P_Num = 0 ,M_Num = 0,T_Num = 0,W_Num = 0;//动物标志简称
uint8_t show_flog = 1;

void Printf_Animal(void)
{
    uint8_t x = 0,y = 0;
    if(show_flog == 1)
    {
for(int i = 0; i < animal_date_num;i++)
    {
        x = animal_date[i].Location/10;
        y = animal_date[i].Location%10;
        //象
        if(animal_date[i].Elephabt > 0 )
        {
            Screen_Num(20+E,y+1);
            Screen_Num(24+E,7-x);
            Screen_Num(E,animal_date[i].Elephabt); 
            if(E == 0)
            {
                E = 1; 
            }
            else if (E == 1)
            {
                E = 2;
            } 
            else if (E == 2)
            {
                E = 3;
            }
        }
        //虎
        if(animal_date[i].Tiger > 0 )
        {
            Screen_Num(30+T,y+1);
            Screen_Num(34+T,7-x); 
            Screen_Num(3+T,animal_date[i].Tiger); 
            if(T == 0)
            {
                T = 1; 
            }
            else if (T == 1)
            {
                T = 2;
            } 
            else if (T == 2)
            {
                T = 3;
            }
        }
        //狼
        if(animal_date[i].Wolf > 0 )
        {
            Screen_Num(40+W,y+1);
            Screen_Num(44+W,7-x);
            Screen_Num(6+W,animal_date[i].Wolf);  
            if(W == 0)
            {
                W = 1; 
            }
            else if (W == 1)
            {
                W = 2;
            } 
            else if (W == 2)
            {
                W = 3;
            }
        }
        //孔雀
        if(animal_date[i].Peacock > 0 )
        {
            Screen_Num(50+P,y+1);
            Screen_Num(54+P,7-x);
            Screen_Num(9+P,animal_date[i].Peacock);  
            if(P == 0)
            {
                P = 1; 
            }
            else if (P == 1)
            {
                P = 2;
            } 
            else if (P == 2)
            {
                P = 3;
            }
        }
        //猴子
        if(animal_date[i].Monkey > 0 )
        {
            Screen_Num(60+M,y+1);
            Screen_Num(64+M,7-x); 
            Screen_Num(12+M,animal_date[i].Monkey); 
            if(M == 0)
            {
                M = 1; 
            }
            else if (M == 1)
            {
                M = 2;
            } 
            else if (M == 2)
            {
                M = 3;
            }
        }
        E_Num += animal_date[i].Elephabt;
        M_Num += animal_date[i].Monkey;
        P_Num += animal_date[i].Peacock;
        W_Num += animal_date[i].Wolf;
        T_Num += animal_date[i].Tiger;
    }

    if(E_Num >= 3)
    {
        E_Num = 3;
    }
    if (T_Num >= 3)
    {
        T_Num = 3;
    }
    if (W_Num >= 3)
    {
        W_Num = 3;
    }
    if (P_Num >= 3)
    {
        P_Num = 3;
    }
    if (M_Num >= 3)
    {
        M_Num = 3;
    }
    Screen_Num(80,E_Num);
    Screen_Num(81,T_Num);
    Screen_Num(82,W_Num); 
    Screen_Num(83,P_Num);
    Screen_Num(84,M_Num);
    show_flog = 0;
    }    
}